package week6.day40.title33;

import java.util.*;

/**
 * @Author fengzihao
 * @Date 2024-05-24 20:41
 */
public class ElectronicBusinessSystem {
    public static void main(String[] args) {
        List<Product> allProducts = generateAllProducts();
        Map<Integer,Product> products = getMapByList(allProducts);
        List<Order> orders = generateAllOrders(products);
        for (Order order : orders) {
            System.out.println("order = " + order);
        }

    }

    private static List<Order> generateAllOrders(Map<Integer,Product> products) {
        List<Order> orders = new ArrayList<>();
        orders.add(new Order(1,new ArrayList<>(Arrays.asList(1,2,3,4,5,6)),120030,products));
        orders.add(new Order(2,new ArrayList<>(Arrays.asList(8,4,5,6)),213123,products));
        orders.add(new Order(3,new ArrayList<>(Arrays.asList(2,4,6)),156464,products));
        orders.add(new Order(4,new ArrayList<>(Arrays.asList(1,2,3,4,5,6)),76457456,products));
        orders.add(new Order(5,new ArrayList<>(Arrays.asList(1,2,6)),456489,products));
        orders.add(new Order(6,new ArrayList<>(Arrays.asList(1,2,3,4,7,8)),567756,products));
        orders.add(new Order(7,new ArrayList<>(Arrays.asList(1,2,5,6)),589953,products));
        return orders;
    }

    private static List<Product> generateAllProducts() {
        List<Product> allProducts = new ArrayList<>();
        allProducts.add(new Product(1,"华为手机",4999));
        allProducts.add(new Product(2,"ipad",3999));
        allProducts.add(new Product(3,"机械革命电脑",8999));
        allProducts.add(new Product(4,"联想电脑",5999));
        allProducts.add(new Product(5,"苹果",5));
        allProducts.add(new Product(6,"香蕉",7));
        allProducts.add(new Product(7,"可乐",3));
        allProducts.add(new Product(8,"T恤",88));
        return allProducts;
    }
    private static Map<Integer, Product> getMapByList(List<Product> allProducts) {
        Map<Integer,Product> hashMap = new HashMap<>();
        for (Product product : allProducts) {
            hashMap.put(product.id,product);
        }
        return hashMap;
    }
}
class Product{
    int id;
    String name;
    double price;

    public Product(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
class Order{
    public int id;
    public List<Integer> productIds;
    public double money;

    public Map<Integer,Product> productsMap;
    public Order(int id, List<Integer> productIds, double money,Map<Integer,Product> products) {
        this.id = id;
        this.productIds = productIds;
        this.money = money;
        getProductsByProductMap(products);
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", productIds=" + productIds +
                ", money=" + money +
                ", productsMap=" + productsMap.toString() +
                '}';
    }

    public void getProductsByProductMap(Map<Integer,Product> products){
        HashMap<Integer, Product> idAndProductHashMap = new HashMap<>();
        for (Integer productId : productIds) {
            idAndProductHashMap.put(productId,products.get(productId));
        }
        productsMap = idAndProductHashMap;
    }
}

